<?php

namespace C\S\Other;

use C\L\Service;
use C\M\ChannelLog;
use C\P\Aiclk;
use C\P\KuaiShou;
use C\P\Baidu;
use C\P\Tencent;
use C\P\Zhihu;

class Channel extends Service
{
    /**
     * 记录渠道商返回的数据
     *
     * @param array $params
     * @return bool
     **/
    public function record($params)
    {
        return (new ChannelLog)->save($params);
    }

    /**
     * 检查是否有匹配的数据
     *
     * @param string $oaid
     * @param string $imei
     * @return bool
     **/
    public function match($oaid, $imei)
    {
        if (!$oaid && !$imei) {
            return false;
        }

        $imeiMD5 = $imei ? strtolower(md5($imei)) : null;

        $expiredTimestamp = mktime(0, 0, 0, date("m"), date("d") - 7, date("Y"));
        $expiredDate = date('Y-m-d H:i:s', $expiredTimestamp);

        $channelLog = ChannelLog::findFirst([
            'conditions' => '(imei = :imei: OR oaid = :oaid:) ' . 'AND created_at >= :expired_date:',
            'order'      => 'id DESC',
            'bind'       => [
                'imei'         => $imeiMD5,
                'oaid'         => $oaid,
                'expired_date' => $expiredDate,
            ],
        ]);

        return $channelLog;
    }

    /**
     * 上报数据至渠道商
     *
     * @param object $channelLog
     * @param string $event
     * @return bool
     **/
    public function report($channelLog, $event)
    {
        if (!$channelLog) {
            return;
        }

        $config = $this->di['config']->get('channel');
        $provider = $config->channels[$channelLog->channel]['provider'] ?? '';

        switch ($provider) {
            case 'aiclk':
                $this->reportByAiclk($channelLog, $event);
                break;
            case 'kuai_shou':
                $this->reportByKuaiShou($channelLog, $event);
                break;
            case 'baidu':
                $this->reportByBaidu($channelLog, $event);
                break;
            case 'tencent':
                $this->reportByTencent($channelLog, $event);
                break;
            case 'zhihu':
                $this->reportByZhihu($channelLog, $event);
                break;
            default:
                break;
        }
    }

    private function reportByAiclk($channelLog, $event)
    {
        $otherInfo = json_decode($channelLog->other_info, true);

        $callbackUrl = $otherInfo['callback_url'];

        return (new Aiclk)->report($callbackUrl, $event);
    }

    private function reportByKuaiShou($channelLog, $event)
    {
        $otherInfo = json_decode($channelLog->other_info, true);

        $callbackUrl = $otherInfo['callback_url'];

        return (new KuaiShou)->report($callbackUrl, $event);
    }

    private function reportByBaidu($channelLog, $event)
    {
        $otherInfo = json_decode($channelLog->other_info, true);

        $callbackUrl = $otherInfo['callback_url'];
        $akey = $otherInfo['akey'];

        return (new Baidu)->report($callbackUrl, $akey, $event);
    }

    private function reportByTencent($channelLog, $event)
    {
        $otherInfo = json_decode($channelLog->other_info, true);

        $config = $this->di['config']->get('channel')->tencent ?? [];

        $driver = new Tencent(
            $config['account_id'],
            $config['access_token'],
            $config['user_action_set_id']
        );

        return $driver->report($config['callback_url'], $event, $otherInfo);
    }

    private function reportByZhihu($channelLog, $event)
    {
        $otherInfo = json_decode($channelLog->other_info, true);

        $callbackUrl = $otherInfo['callback_url'];

        return (new Zhihu)->report($callbackUrl, $event);
    }
}
